﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataLayer.Infrastructure;
using ModelLayer;

namespace BusinessLayer
{
    public class UserCartManager
    {

        public IEnumerable<UserCart> GetAllUserCarts()
        {
            using (var uow = new UnitOfWork())
            {
                return uow.UserCartRepo.Get();
            }
        }

        public IEnumerable<UserCart> GetByUser(long userId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.UserCartRepo.Get(x => x.UserId == userId, null, x => x.Product);
            }
        }

        public UserCart GetById(long cartId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.UserCartRepo.GetById(cartId);
            }
        }

        public bool AddUpdate(UserCart userCart)
        {
            using (var uow = new UnitOfWork())
            {
                if (userCart.CartId > 0)
                {

                }
                else
                {
                    uow.UserCartRepo.Insert(userCart);
                }

                return uow.SaveChanges() > 0;
            }
        }

        public bool Delete(long cartId)
        {
            using (var uow = new UnitOfWork())
            {
                var userCart = uow.UserCartRepo.GetById(cartId);
                uow.UserCartRepo.Delete(userCart);

                return uow.SaveChanges() > 0;
            }
        }

        public bool ClearCart(long userId)
        {
            using (var uow = new UnitOfWork())
            {
                var userCarts = uow.UserCartRepo.Get(x => x.UserId == userId);
                foreach (var userCart in userCarts)
                {
                    uow.UserCartRepo.Delete(userCart);
                }

                return uow.SaveChanges() > 0;
            }
        }
    }
}
